
// A1_summarize_estimates
//==============================================================================

// Description: This file formats and summarizes the estimates for quality q

clear
eststo clear
set more off

cd "D:\data_replication"


// 1. Medium and Larger countries only (> 1,000 observations)
//------------------------------------------------------------------------------

use statistics_and_graphs\quality\quality_summary.dta, clear

sort partner
by partner: gen count = _N
keep if count > 1000

gen ptq = product_id * 100000 + year * 10  + quarter + declarant * 100

xtset ptq
eststo: xtreg q_k i.partner , fe

esttab using statistics_and_graphs\quality\output_files\reg_q_partner.tex, replace label nostar ///
title(Regression table\label{tab1})

esttab using statistics_and_graphs\quality\output_files\reg_q_partner.csv, replace wide label nostar ///
title(Regression table\label{tab1})


import delimited statistics_and_graphs\quality\output_files\reg_q_partner.csv, clear 

replace v1 = subinstr(v1,"=","",1)
replace v1 = subinstr(v1,"=","",1)
replace v1 = subinstr(v1,"Partner","",1)
replace v1 = subinstr(v1, char(34),"",1)
replace v1 = subinstr(v1, char(34),"",1)

replace v2 = subinstr(v2,"=","",1)
replace v2 = subinstr(v2, char(34),"",1)
replace v2 = subinstr(v2, char(34),"",1)

drop if _n <  4
drop if _n >  100
drop v3

destring v1, replace
destring v2, replace

rename v1 partner
rename v2 q
gen year = 2010
merge 1:1 partner year using data\gdp_per_capita\gdp_per_capita_defl.dta
keep if _merge == 3
drop _merge
merge 1:1 partner using data\country_codes\ISO3_partner_codes.dta
keep if _merge == 3
drop _merge
gen gdp_log = log(gdp_per_capita)

export delimited statistics_and_graphs\quality\output_files\q_partner_1000.csv, replace

twoway (scatter q gdp_log)

regress q gdp_log, vce(robust)


// 2. Large countries only (> 5,000 observation)
//------------------------------------------------------------------------------

eststo clear
use statistics_and_graphs\quality\quality_summary.dta, clear

sort partner
by partner: gen count = _N
keep if count > 5000

gen ptq = product_id * 100000 + year * 10  + quarter + declarant * 100

xtset ptq
eststo: xtreg q_k i.partner , fe

esttab using statistics_and_graphs\quality\output_files\reg_q_partner.tex, replace label nostar ///
title(Regression table\label{tab1})

esttab using statistics_and_graphs\quality\output_files\reg_q_partner.csv, replace wide label nostar ///
title(Regression table\label{tab1})


import delimited statistics_and_graphs\quality\output_files\reg_q_partner.csv, clear 

replace v1 = subinstr(v1,"=","",1)
replace v1 = subinstr(v1,"=","",1)
replace v1 = subinstr(v1,"Partner","",1)
replace v1 = subinstr(v1, char(34),"",1)
replace v1 = subinstr(v1, char(34),"",1)

replace v2 = subinstr(v2,"=","",1)
replace v2 = subinstr(v2, char(34),"",1)
replace v2 = subinstr(v2, char(34),"",1)

drop if _n <  4
drop if _n >  71
drop v3

destring v1, replace
destring v2, replace

rename v1 partner
rename v2 q
gen year = 2010
merge 1:1 partner year using data\gdp_per_capita\gdp_per_capita_defl.dta
keep if _merge == 3
drop _merge
merge 1:1 partner using data\country_codes\ISO3_partner_codes.dta
keep if _merge == 3
drop _merge
gen gdp_log = log(gdp_per_capita)

export delimited statistics_and_graphs\quality\output_files\q_partner_5000.csv, replace

twoway (scatter q gdp_log)

regress q gdp_log, vce(robust)



// 3. Only exclude very small countries (< 100 observations)
//------------------------------------------------------------------------------

eststo clear
use statistics_and_graphs\quality\quality_summary.dta, clear

sort partner
by partner: gen count = _N
keep if count > 100

gen ptq = product_id * 100000 + year * 10  + quarter + declarant * 100

xtset ptq
eststo: xtreg q_k i.partner , fe

esttab using statistics_and_graphs\quality\output_files\reg_q_partner.tex, replace label nostar ///
title(Regression table\label{tab1})

esttab using statistics_and_graphs\quality\output_files\reg_q_partner.csv, replace wide label nostar ///
title(Regression table\label{tab1})


import delimited statistics_and_graphs\quality\output_files\reg_q_partner.csv, clear 

replace v1 = subinstr(v1,"=","",1)
replace v1 = subinstr(v1,"=","",1)
replace v1 = subinstr(v1,"Partner","",1)
replace v1 = subinstr(v1, char(34),"",1)
replace v1 = subinstr(v1, char(34),"",1)

replace v2 = subinstr(v2,"=","",1)
replace v2 = subinstr(v2, char(34),"",1)
replace v2 = subinstr(v2, char(34),"",1)

drop if _n <  4
drop if _n >  165
drop v3

destring v1, replace
destring v2, replace

rename v1 partner
rename v2 q
gen year = 2010
merge 1:1 partner year using data\gdp_per_capita\gdp_per_capita_defl.dta
keep if _merge == 3
drop _merge
merge 1:1 partner using data\country_codes\ISO3_partner_codes.dta
keep if _merge == 3
drop _merge
gen gdp_log = log(gdp_per_capita)

export delimited statistics_and_graphs\quality\output_files\q_partner_100.csv, replace

